'''
Created on Jan 21, 2013

@author: Orkun
'''
import argparse, urllib
from BeautifulSoup import BeautifulSoup
from EmailSender import mail

def executeAlert(address, key):        
    if htmlSearch(address, key)==1:        
        print 'success!! '
        mail('skeletorkun@gmail.com','test', 'weeee!!!')        
    else:
        print 'fail .. '
    
    
def main():
    # input arguments
    parser = argparse.ArgumentParser(description=' Search website for a keyword ')
    parser.add_argument('address', help='the address of the target site')
    parser.add_argument('key', help='keyword to look for')    
    args = parser.parse_args()    
    executeAlert(args.address, args.key)    
    
def htmlSearch(address, key):
    f = urllib.urlopen(str(address))
    html = f.read()
    soup = BeautifulSoup(html)
    #print soup.prettify()    
    if soup.findAll(text=key, limit=1):        
        return 1
    return 0
            
if __name__ == '__main__':
    main()
    